﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using QuickDev.Common.DB;
using QuickDev.Common.Utils;

using QuickDev.Model;

#endregion

namespace QuickDev.SQLServerDAL
{
    /// <summary>
    /// Data Access Layer For dbo.OPM_ReturnOrderMemo.
    /// </summary>
    public class ReturnOrderMemoDAL
    {
        #region GetModel

        /// <summary>
        /// public ReturnOrderMemo GetModel( int id )
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>ReturnOrderMemo model</returns>
        public static ReturnOrderMemo GetModel( int id )
        {
            string procedureName = "OPM_ReturnOrderMemo_GetModel";

            ReturnOrderMemo model = new ReturnOrderMemo();

            SqlParameter parameter = new SqlParameter( "@Id" , id );

            try
            {
                ReaderFactory.ReaderToModel<ReturnOrderMemo>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameter ) , model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region GetList

        /// <summary>
        /// public List(ReturnOrderMemo) GetList( bool showHidden , bool showDeleted , int returnOrderId )
        /// </summary>
        /// <param name="showHidden">showHidden</param>
        /// <param name="showDeleted">showDeleted</param>
        /// <param name="returnOrderId">returnOrderId</param>
        /// <returns>List(ReturnOrderMemo)</returns>
        public static List<ReturnOrderMemo> GetList( bool showHidden , bool showDeleted , int returnOrderId )
        {
            string procedureName = "OPM_ReturnOrderMemo_GetList";

            List<ReturnOrderMemo> modelList = new List<ReturnOrderMemo>();

            SqlParameter[] parameters = {
					new SqlParameter( "@ShowHidden", SqlDbType.Bit, 1 ),
                    new SqlParameter( "@ShowDeleted", SqlDbType.Bit, 1 ),
                    new SqlParameter( "@ReturnOrderId", SqlDbType.Int, 4 )
				};
            parameters[0].Value = showHidden;
            parameters[1].Value = showDeleted;
            parameters[2].Value = returnOrderId;

            try
            {
                ReaderFactory.ReaderToList<ReturnOrderMemo>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameters ) , modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Insert

        /// <summary>
        /// public static int Insert( int returnOrderId, string memo, int createdBy )
        /// </summary>
        /// <param name="returnOrderId">returnOrderId</param>
        /// <param name="memo">memo</param>
        /// <param name="createBy">createBy</param>
        /// <returns>Id</returns>
        public static int Insert( int returnOrderId , string memo , int createdBy )
        {
            string procedureName = "OPM_ReturnOrderMemo_Insert";

            SqlParameter[] parameters = {
				new SqlParameter("@ReturnOrderId", SqlDbType.Int,4),	
				new SqlParameter("@Memo", SqlDbType.NVarChar,4000),	
				new SqlParameter("@CreatedBy", SqlDbType.Int,4),
                new SqlParameter("@Id", SqlDbType.Int,4)
			};

            int nIndex = 0;
            parameters[nIndex++].Value = returnOrderId;
            parameters[nIndex++].Value = memo;
            parameters[nIndex++].Value = createdBy;
            parameters[nIndex].Direction = ParameterDirection.Output;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return (int)Utility.ChangeTypeTo( parameters[nIndex].Value , typeof( Int32 ) );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Update

        /// <summary>
        /// public static bool UpdateStatus( int id , bool published , int updatedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="published">published</param>
        /// <param name="updatedBy">updatedBy</param>
        /// <returns>bool successful</returns>
        public static bool UpdateStatus( int id , bool published , int updatedBy )
        {
            string procedureName = "OPM_ReturnOrderMemo_UpdateStatus";

            SqlParameter[] parameters = {
				new SqlParameter("@Id", SqlDbType.Int,4),	
				new SqlParameter("@Published", SqlDbType.Bit,1),	
				new SqlParameter("@UpdatedBy", SqlDbType.Int,4),
                new SqlParameter()
			};

            int nIndex = 0;
            parameters[nIndex++].Value = id;
            parameters[nIndex++].Value = published;
            parameters[nIndex++].Value = updatedBy;
            parameters[nIndex].Direction = ParameterDirection.ReturnValue;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[nIndex].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Delete

        /// <summary>
        /// public bool Delete( int id, int deletedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="int deletedBy ">int deletedBy </param>
        /// <returns>bool successful</returns>
        public static bool Delete( int id , int deletedBy )
        {
            string procedureName = "OPM_ReturnOrderMemo_Delete";

            SqlParameter[] parameters = {
                new SqlParameter("@Id", SqlDbType.Int, 4),
                new SqlParameter("@DeletedBy", SqlDbType.Int, 4),
                new SqlParameter()
			};

            parameters[0].Value = id;
            parameters[1].Value = deletedBy;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[2].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion
    }
}

